package com.eliza.demo.qq.fg

import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.ImageButton
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.navigation.Navigation
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.eliza.demo.R
import com.eliza.demo.qq.activity.QQAdapter
import com.eliza.demo.vm.QQUserVm
import com.eliza.library.dao.ContactorDao
import com.eliza.library.dao.ContactorDatabase
import com.eliza.library.dao.QQUserDao
import com.eliza.library.dao.QQUserDatabase
import com.eliza.library.model.Contactor
import com.eliza.library.model.QQUser
import com.eliza.library.tools.InfoTools
import kotlinx.coroutines.launch

class QQRegisterFragment : Fragment() {
    lateinit var qqAdapter: QQAdapter
    lateinit var qqUserDao: QQUserDao
    var parentContext: Context? = null
    lateinit var holder: Holder
    lateinit var quv: QQUserVm
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        var view = inflater.inflate(R.layout.fragment_qq_register, container, false)

        // Inflate the layout for this fragment
        parentContext = parentFragment?.context ?: null
        val parentApplication = parentFragment?.activity?.application ?: null
        holder = Holder(view)

        /*
                   * 跳转界面
                   * */
        holder.img_back.setOnClickListener {

            Navigation.findNavController(view).apply {
                navigate(R.id.action_QQRegisterFragment_to_QQLoginFragment)
            }
        }
        parentContext?.let {
            qqUserDao = QQUserDatabase.getInstance(it).qqUserDao
            val recycleView = view.findViewById<RecyclerView>(R.id.qq_register_recycle)
            recycleView.layoutManager = LinearLayoutManager(context)
            qqAdapter = QQAdapter(
                listOf<QQUser>(QQUser("name", "as"))
            )
            recycleView.adapter = qqAdapter
            parentApplication?.let { application ->
                quv = ViewModelProvider(
                    this, ViewModelProvider.AndroidViewModelFactory(application)
                ).get(QQUserVm::class.java)
                setAllContactorsLive(quv)
            }
        }
        holder.register_btn.setOnClickListener {
//            数据库方法
            lifecycleScope.launch {
                mInsert()
            }
            mQuery()
        }

        return view
    }

    fun setAllContactorsLive(cdv: QQUserVm) {

        cdv.getAllQQUsersLive().observe(viewLifecycleOwner) {
            qqAdapter.qqUserList = it
            qqAdapter.notifyDataSetChanged()
        }
    }

    private suspend fun mInsert() {
        qqUserDao.insertQQUser(
            QQUser(
                holder.et_name.text.toString(),
                holder.et_pwd.text.toString(),
            )
        )
    }

    fun mQuery(): LiveData<List<QQUser>> {
        InfoTools.LogTools(this::class.java.name, qqUserDao.getAllQQUserList().value?.size ?: 0)
        return qqUserDao.getAllQQUserList()
    }

    class Holder(view: View) {
        val et_name = view.findViewById<EditText>(R.id.qq_register_et_name)
        val et_pwd = view.findViewById<EditText>(R.id.qq_register_et_pwd)
        val register_btn = view.findViewById<ImageButton>(R.id.qq_register_btn)
        val img_back = view.findViewById<ImageButton>(R.id.qq_register_img_back)

    }


}